﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class ePioneer_Lecturer
    {
        readonly LYM.CommonWeb.DataServer.DataBase data = new LYM.CommonWeb.DataServer.DataBase(LYM.CommonWeb.ConfigHelper.GetConfigString("EMPC"), false);
        public ePioneer_Lecturer()
        { }
        #region  Method


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.ePioneer_Lecturer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into ePioneer_Lecturer(");
            strSql.Append("Lecturer,BriefInfo,PicUrl,Location)");
            strSql.Append(" values (");
            strSql.Append("@Lecturer,@BriefInfo,@PicUrl,@Location)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Lecturer", SqlDbType.NVarChar,50),
					new SqlParameter("@BriefInfo", SqlDbType.NVarChar),
					new SqlParameter("@PicUrl", SqlDbType.NVarChar,1000),
					new SqlParameter("@Location", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.Lecturer;
            parameters[1].Value = model.BriefInfo;
            parameters[2].Value = model.PicUrl;
            parameters[3].Value = model.Location;

            object obj = null;
            data.GetValue(strSql.ToString(), false,ref obj, parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.ePioneer_Lecturer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update ePioneer_Lecturer set ");
            strSql.Append("Lecturer=@Lecturer,");
            strSql.Append("BriefInfo=@BriefInfo,");
            strSql.Append("PicUrl=@PicUrl,");
            strSql.Append("Location=@Location");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Lecturer", SqlDbType.NVarChar,50),
					new SqlParameter("@BriefInfo", SqlDbType.NVarChar),
					new SqlParameter("@PicUrl", SqlDbType.NVarChar,1000),
					new SqlParameter("@Location", SqlDbType.NVarChar,50),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Lecturer;
            parameters[1].Value = model.BriefInfo;
            parameters[2].Value = model.PicUrl;
            parameters[3].Value = model.Location;
            parameters[4].Value = model.Id;

            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false,ref rows, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ePioneer_Lecturer ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
};
            parameters[0].Value = Id;

            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false,ref rows, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from ePioneer_Lecturer ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = 0;
            data.ProExecuteCommand(strSql.ToString(), false,ref rows, null);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.ePioneer_Lecturer GetModel(string lecturer)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from ePioneer_Lecturer ");
            strSql.Append(" where Lecturer=@Lecturer");
            SqlParameter[] parameters = {
					new SqlParameter("@Lecturer",lecturer)
};

            Model.ePioneer_Lecturer model = new Model.ePioneer_Lecturer();
            DataTable dt = new DataTable();
            data.GetDataTable(strSql.ToString(), false,ref dt, parameters);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["Id"] != null && dt.Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(dt.Rows[0]["Id"].ToString());
                }
                if (dt.Rows[0]["Lecturer"] != null && dt.Rows[0]["Lecturer"].ToString() != "")
                {
                    model.Lecturer = dt.Rows[0]["Lecturer"].ToString();
                }
                if (dt.Rows[0]["BriefInfo"] != null && dt.Rows[0]["BriefInfo"].ToString() != "")
                {
                    model.BriefInfo = dt.Rows[0]["BriefInfo"].ToString();
                }
                if (dt.Rows[0]["PicUrl"] != null && dt.Rows[0]["PicUrl"].ToString() != "")
                {
                    model.PicUrl = dt.Rows[0]["PicUrl"].ToString();
                }
                if (dt.Rows[0]["Location"] != null && dt.Rows[0]["Location"].ToString() != "")
                {
                    model.Location = dt.Rows[0]["Location"].ToString();
                }
                if (dt.Rows[0]["Speciality"] != null && dt.Rows[0]["Speciality"].ToString() != "")
                {
                    model.Speciality = dt.Rows[0]["Speciality"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM ePioneer_Lecturer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false,ref ds, null);
            return ds;
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" * ");
            strSql.Append(" FROM ePioneer_Lecturer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            DataSet ds = new DataSet();
            data.GetDataSet(strSql.ToString(), false,ref ds, null);
            return ds;
        }

        #endregion  Method
    }
}
